# -*- coding: utf-8 -*-
from helpers.logger import Logger
from helpers.not_youtube import NotYoutube
from helpers.player import Player
from pyfzf.pyfzf import FzfPrompt
import os

subscriptions_list = open('subscribe.txt', 'r').readlines()

channel_list = {}
prev_h = "30"  # высота для превью
prev_w = "90"  # ширина для превью
cache_path = "{0}/cache/".format(os.getcwd())
n_you = NotYoutube(cache_path)

Logger.log_info("Загрузка каналов....")
for channel_url in subscriptions_list:
    channel_url = channel_url.strip()
    name = n_you.get_channel_name(channel_url)
    if name == "":
        continue
    Logger.log_info("found info about channel", channel_url, name)
    channel_list[name] = channel_url

fzf = FzfPrompt()
target_channel = fzf.prompt(channel_list)
Logger.log_info("selected channel:", target_channel[0])

video_list = n_you.get_channel_video(channel_list.get(target_channel[0]))
target_video = fzf.prompt(video_list, "--preview 'viu -w "+prev_w+" -h "+prev_h+" $(python3 preview_calc.py {})'")
Logger.log_info("selected video from channel:", target_video[0])
video_url = video_list.get(target_video[0])

Player.play_mpv("https://www.youtube.com" + video_url)
